package cn.sdormitory.job.dormitory;

import cn.sdormitory.attendance.dao.HolidayConfigDao;
import cn.sdormitory.attendance.entity.HolidayConfig;
import cn.sdormitory.common.sms.TxSmsTemplate;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.http.client.utils.DateUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;
import java.util.List;

/**
 * @author: lx
 * @date: 2023/9/15 11:11
 * @description: 宿舍考勤通知部门经理Job：部门经理管辖部门下的所有班级未归寝学生数量
 */
public class DormitoryAttendanceNoticeDeptManagerJob extends QuartzJobBean {

    @Autowired
    private SdAttenceDao sdAttenceDao;

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private TxSmsTemplate txSmsTemplate;
    
    @Autowired
    private HolidayConfigDao holidayConfigDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date date = new Date();
        String accessDate = DateUtils.formatDate(date, "yyyy-MM-dd");
        
//        accessDate = "2024-09-27";
    
        //获取所有假期 如果当天在假期之内 则匹配是否该部门放假 true则放假不发送短信
        List<HolidayConfig> holiday = holidayConfigDao.getHolidayByDate(accessDate);
        // 假期配置中有该部门  不是月假 不是月假最后一天
        boolean qmx = holiday.stream().anyMatch(holidayConfig -> "12".equals(String.valueOf(holidayConfig.getDeptId()))
                && (holidayConfig.getIsHoliday().equals("0") || holidayConfig.getHolidayEnd().equals(accessDate)));
        boolean bcsp = holiday.stream().anyMatch(holidayConfig -> "6".equals(String.valueOf(holidayConfig.getDeptId())) && (holidayConfig.getIsHoliday().equals("0") || holidayConfig.getHolidayEnd().equals(accessDate)));
        boolean sx = holiday.stream().anyMatch(holidayConfig -> "7".equals(String.valueOf(holidayConfig.getDeptId())) && (holidayConfig.getIsHoliday().equals("0") || holidayConfig.getHolidayEnd().equals(accessDate)));
    
        if (qmx) {
//        查询启蒙星
            Integer QMXStuCount = sdAttenceDao.getAbsenceStuByDeptAndDept(accessDate, "12");
            QueryWrapper<SysUser> QMXUserWrapper = new QueryWrapper<>();
            QMXUserWrapper.eq("username", "张园园");
            SysUser QMXDeptManager = sysUserDao.selectOne(QMXUserWrapper);
            if (QMXDeptManager != null) {
                if (QMXDeptManager.getPhone() != null) {
                    String[] params = {accessDate, "技工", String.valueOf(QMXStuCount) + "人"};
                txSmsTemplate.sendMesModel(QMXDeptManager.getPhone(),params,4);
                }
            }
        }
    
        if (bcsp) {
//        查询BCSP
            Integer BCSPStuCount = sdAttenceDao.getAbsenceStuByDeptAndDept(accessDate, "6");
            QueryWrapper<SysUser> BCSPUserWrapper = new QueryWrapper<>();
            BCSPUserWrapper.eq("username", "汤金霞");
            SysUser BCSPDeptManager = sysUserDao.selectOne(BCSPUserWrapper);
            if (BCSPDeptManager != null) {
                if (BCSPDeptManager.getPhone() != null) {
                    String[] params = {accessDate, "BCSP", String.valueOf(BCSPStuCount) + "人"};
                txSmsTemplate.sendMesModel(BCSPDeptManager.getPhone(),params,4);
                }
            }
        
        }
        if (sx) {
//        查询实训
            Integer SXStuCount = sdAttenceDao.getAbsenceStuByDeptAndDept(accessDate, "7");
            QueryWrapper<SysUser> SXUserWrapper = new QueryWrapper<>();
            SXUserWrapper.eq("username", "黄群英");
            SysUser SXDeptManager = sysUserDao.selectOne(SXUserWrapper);
            if (SXDeptManager != null) {
                if (SXDeptManager.getPhone() != null) {
                    String[] params = {accessDate, "实训部", String.valueOf(SXStuCount) + "人"};
                txSmsTemplate.sendMesModel(SXDeptManager.getPhone(),params,4);
                }
            }
        }
    }
}
